﻿Imports Q7P1TTS.Lib

Public Class EmployeeList

    Dim currentPage, maxPage, rowPerPage As Integer
    Dim dynamicSource As New List(Of Employee)
    Private Sub EmployeeList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboDesignationSelector.SelectedIndex = 0
        cboDeptSelector.DataSource = DepartmentController.load("All", 0)
        rowPerPage = Convert.ToInt32(My.Settings.ROW_PER_PAGE)
        DataBind()
    End Sub

    Private Sub DataBind()
        Dim items As List(Of Employee) = EmployeeController.load().OrderByDescending(Function(emp As Employee) emp.ID).ToList()
        If cboDeptSelector.SelectedValue > 0 Then
            items = items.Where(Function(i As Employee) i.DeptID = cboDeptSelector.SelectedValue).ToList()
        End If

        If Not String.IsNullOrEmpty(cboDesignationSelector.SelectedItem.ToString()) And cboDesignationSelector.SelectedItem.ToString() <> "All" Then
            items = items.Where(Function(i As Employee) i.Designation = cboDesignationSelector.SelectedItem.ToString()).ToList()
        End If

        If Not String.IsNullOrEmpty(txtSearch.Text) Then
            Dim regEx As New System.Text.RegularExpressions.Regex(txtSearch.Text)
            items = items.Where(Function(i As Employee) regEx.IsMatch(i.FirstName) Or regEx.IsMatch(i.LastName) Or regEx.IsMatch(i.Email) Or regEx.IsMatch(i.Account)).Distinct().ToList()
        End If
        grdEmployeeList.AutoGenerateColumns = False
        grdEmployeeList.DataSource = items
        dynamicSource = items
        maxPage = Math.Ceiling(dynamicSource.Count / rowPerPage)
        cboPageSelector.Items.Clear()
        For i As Integer = 1 To maxPage Step 1
            cboPageSelector.Items.Add(i)
        Next
        If cboPageSelector.Items.Count > 0 Then
            cboPageSelector.SelectedIndex = 0
        End If
        Paging()
    End Sub

    Private Sub Paging()
        lblMaxPage.Text = "/" & maxPage.ToString()
        grdEmployeeList.DataSource = dynamicSource.Skip((currentPage - 1) * rowPerPage).Take(rowPerPage).ToList()

        btnFirst.Enabled = (currentPage > 1)
        btnPrev.Enabled = (currentPage > 1)
        btnNext.Enabled = (currentPage < maxPage)
        btnLast.Enabled = (currentPage < maxPage)
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        DataBind()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim main As MainForm = Me.FindForm()
        main.GoToForm(New EmployeeForm())
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Dim main As MainForm = Me.FindForm()
        If grdEmployeeList.SelectedRows.Count <> 1 Then
            MessageBox.Show("Please select 1 record.")
        Else
            Dim obj As Employee = grdEmployeeList.SelectedRows(0).DataBoundItem
            main.GoToForm(New EmployeeForm(obj.ID))
        End If
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        cboPageSelector.SelectedIndex = 0
    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        cboPageSelector.SelectedIndex = cboPageSelector.SelectedIndex - 1
    End Sub

    Private Sub cboPageSelector_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPageSelector.SelectedIndexChanged
        currentPage = Convert.ToInt32(cboPageSelector.SelectedItem)
        Paging()
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        cboPageSelector.SelectedIndex = cboPageSelector.SelectedIndex + 1
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        cboPageSelector.SelectedIndex = cboPageSelector.Items.Count - 1
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If grdEmployeeList.SelectedRows.Count <> 1 Then
            MessageBox.Show("Please select 1 record.")
        Else
            If MessageBox.Show("Are you sure", "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Dim obj As Employee = grdEmployeeList.SelectedRows(0).DataBoundItem
                If EmployeeController.delete(obj) > 0 Then
                    DataBind()
                Else
                    MessageBox.Show("There is an unknown error when deleting data. Please contact administrator.")
                End If

            End If
        End If
    End Sub
End Class
